/*
 * Copyright (C) 2018 yixun All Rights Reserved.
 * 
 * OtherCompanyServiceImpl.java
 */
package com.yixun.qfbao.dao.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.mapper.CompanyMapper;
import com.yixun.qfbao.mapper.CompanyRelationMapper;
import com.yixun.qfbao.mapper.OtherCompanyMapper;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.CompanyRelation;
import com.yixun.qfbao.model.OtherCompany;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.CompanyRelationDto;
import com.yixun.qfbao.model.dto.OtherCompanyDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.dao.OtherCompanyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


@Service("otherCompanyDao")
public class OtherCompanyDaoImpl implements OtherCompanyDao {
	
	@Autowired
	private OtherCompanyMapper mapper;
	@Autowired
	private CompanyMapper companyMapper;
	@Autowired
	private CompanyRelationMapper companyRelationMapper;
	
	@Transactional(readOnly = true)
	public Page<OtherCompany> getPageModel(OtherCompanyDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<OtherCompany> rows = mapper.list(entity,begin,end);
	    Page<OtherCompany> msr = new Page<OtherCompany>(total, rows);
		return msr;
	}

	public List<OtherCompany> selectListByDto(OtherCompanyDto entity){
		return mapper.selectListByDto(entity);
	}
	
	public OtherCompany selectByDto(OtherCompanyDto entity){
		return mapper.selectByDto(entity);
	}
	
	public OtherCompany selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}

	public OtherCompanyDto create(OtherCompanyDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	public int update(OtherCompanyDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	public int destroy(OtherCompanyDto entity) {
		return mapper.del(entity);
	}
	
	public int saveOrUpdate(OtherCompanyDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	@Override
	public OtherCompany selectBy(OtherCompany entity) {
		if(entity!=null&&entity.getId()!=null){
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	public List<OtherCompany> getV10CompanyList(String domain){
		CompanyDto company = new CompanyDto();
		company.setDomain(domain);
		company.setDelFlag(DelFlagEnum.UNDEL.getNo());
		Company c = companyMapper.selectByDto(company);
		if(c == null){
			return null;
		}
		CompanyRelationDto companyRelationDto = new CompanyRelationDto();
		companyRelationDto.setDevCompany(c.getId());
		companyRelationDto.setDelFlag(DelFlagEnum.UNDEL.getNo());

		List<CompanyRelation> companyRelationList = companyRelationMapper.selectListByDto(companyRelationDto);
		if(null == companyRelationList || companyRelationList.size() == 0){
			return new ArrayList<>();
		}

		//经济公司的id
		List<Long> companyIds = new ArrayList<Long>();
		for(CompanyRelation cr : companyRelationList){
			companyIds.add(cr.getAgentCompany());
		}

		OtherCompanyDto otherCompanyDto = new OtherCompanyDto();
		otherCompanyDto.setIsDeleted(DelFlagEnum.UNDEL.getNo());
		otherCompanyDto.setChannelCompanyIds(companyIds);
		List<OtherCompany> list = mapper.selectListByDto(otherCompanyDto);


		return list;
	}

	@Override
	public List<OtherCompany> getV10CompanyListByCompanyId(Long companyId) {
		
		CompanyRelationDto companyRelationDto = new CompanyRelationDto();
		companyRelationDto.setDevCompany(companyId);
		companyRelationDto.setDelFlag(DelFlagEnum.UNDEL.getNo());

		List<CompanyRelation> companyRelationList = companyRelationMapper.selectListByDto(companyRelationDto);
		if(null == companyRelationList || companyRelationList.size() == 0){
			return new ArrayList<>();
		}

		//经济公司的id
		List<Long> companyIds = new ArrayList<Long>();
		for(CompanyRelation cr : companyRelationList){
			companyIds.add(cr.getAgentCompany());
		}

		OtherCompanyDto otherCompanyDto = new OtherCompanyDto();
		otherCompanyDto.setIsDeleted(DelFlagEnum.UNDEL.getNo());
		otherCompanyDto.setChannelCompanyIds(companyIds);
		List<OtherCompany> list = mapper.selectListByDto(otherCompanyDto);


		return list;
	}

	public Company getDevCompanyWithV10CompanyUuid(String companyUuid){
		OtherCompanyDto otherCompany = new OtherCompanyDto();
		otherCompany.setCompanyUuid(companyUuid);
		otherCompany.setIsDeleted("0");
		List<OtherCompany> otherCompanyList = mapper.selectListByDto(otherCompany);
		if(otherCompanyList ==null && otherCompanyList.size() == 0){
			return null;
		}
		//获取公司
		CompanyRelationDto companyRelationDto = new CompanyRelationDto();
		companyRelationDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		companyRelationDto.setAgentCompany(otherCompanyList.get(0).getChannelCompanyId());
		List<CompanyRelation> companyRelationList = companyRelationMapper.selectListByDto(companyRelationDto);
		if(companyRelationList ==null && companyRelationList.size() == 0){
			return null;
		}
		Company company = companyMapper.selectById(companyRelationList.get(0).getDevCompany());
		return company;
	}
}
